package com.cliente;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.conver.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import domain.AdministradorVO;
import domain.ClienteVO;
import domain.LoginVO;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class AddCliente extends Activity {

	private static final String METHOD_NAME = "registrarCliente";
	private static final String NAMESPACE = new AndroidController().getNamespace();
	private static final String SOAP_ACTION = "http://webservice/registrarCliente";
	private static final String URL = new AndroidController().getUrl();
	private String TAG = "Beto";// para mostrar cosas en consola con ese tag

	TextView rut, nombre, numero, correo, direccion, apaterno, amaterno;
	Button add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_cliente);

		rut = (TextView) findViewById(R.id.rut);
		nombre = (TextView) findViewById(R.id.nombre1);
		apaterno = (TextView) findViewById(R.id.apaterno);
		amaterno = (TextView) findViewById(R.id.amaterno);
		numero = (TextView) findViewById(R.id.celular);
		correo = (TextView) findViewById(R.id.email);
		direccion = (TextView) findViewById(R.id.direccion);
		add = (Button) findViewById(R.id.addclientebutton);

		add.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				add.setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						// creamos un objeto cliente, lo pasamos a JSON y lo
						// enviamos a ws
						
						SharedPreferences prefs = getSharedPreferences("session",Context.MODE_PRIVATE);
						domain.ClienteVO cliente = new domain.ClienteVO();
						domain.LoginVO lToken =new LoginVO();
						lToken.setToken(prefs.getString("TOKEN", "vacio"));
						
						cliente.setLoginVO(lToken);
						cliente.setRut(rut.getText().toString());
						cliente.setNombres(nombre.getText().toString());
						cliente.setTelefono(numero.getText().toString());
						cliente.setEmail(correo.getText().toString());
						cliente.setDireccion(direccion.getText().toString());
						cliente.setApellido_paterno(apaterno.getText()
								.toString());
						cliente.setApellido_materno(amaterno.getText()
								.toString());

						final Gson gson = new Gson();
						String[] clienteJSON = { gson.toJson(cliente) };

						new AsyncCallWS().execute(clienteJSON);
					}
				});

				// e inmediatamente vamos a la proxima ventana.

			}
		});

	}

	private class AsyncCallWS extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Log.i(TAG, "trabajando");
			return agregar(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			
			Intent main=new Intent(AddCliente.this, Menu.class);
			Bundle test = new Bundle();
			test.putString("response", result);
			main.putExtras(test);
			startActivity(main);
			finish();

		}
	}

	public String agregar(String clienteJson) {

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		// agregando parametro
		request.addProperty("clienteJSON", clienteJson.toString());

		// configurando envio
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			Log.i("Webservice Output", response.toString());

			return response.toString();

		} catch (Exception e) {
			e.printStackTrace();
			Log.i(TAG, "" + e);
		}

		return null;
	}

}
